#ifndef mW_map_include
#define mW_map_include

#include <allegro.h>
#include <stdlib.h>
#include <stdio.h>
#include "tile.h"
#include "utility.h"



#define mW_MAP_SCROLL_SPEED 4

#define mW_MAP_LOCATION_BIT_PASSABLE 1

#define mW_MAP_SHOW_PASSABLE 1


/*
 * flags
 * 0000 000X : 0 non-passable, 1 passable
 * 0000 00X0 : event attached to this tile
 * 0000 0X00 : this tile gets blitted over sprites
 * X000 0000 : this tile is followed by another tile (used mainly for map storage)
 */
typedef struct _mW_map_location
{
	
	unsigned short tile_id;
	unsigned char flags;
	struct _mW_map_location * next_location;
	
} mW_map_location;



/*
 * flags
 * 0000 000X : 1 Show passable status of tiles, 0 Do not show status
 */
typedef struct
{

	// dimensions of the map
	int width;
	int height;
	
	// The number of tiles wide and high the map is when it is displayed.
	// These measurements are used for centering the map on objects
	int display_width;
	int display_height;

	// the location of the top and the left of the map
	int left;
	int top;
	
	// pixel offsets for scrolling
	int offset_x;
	int offset_y;
	
	// various flags for the map
	unsigned char flags;
	
	mW_map_location *** locations;
	
} mW_map;


// annoying, but has to go here until we move mW_map struct variables from game.h
// considering making those struct variables statics in game.c
#include "game.h"




// the offset from the left and top of the current map in pixels



int mW_map_blit (mW_map * map, BITMAP * surface, int map_width, int map_height, int surface_x, int surface_y);

/*
 * Purpose : Centers the map on a tile
 * Returns : 1 when complete
 * mW_error: N/A
 */
int mW_map_center(mW_map * map, int x, int y);

mW_map * mW_map_create (int width, int height);
int mW_map_destroy (mW_map * map);

unsigned int mW_map_get_x_pixel (mW_map * map, unsigned short x);
unsigned int mW_map_get_y_pixel (mW_map * map, unsigned short y);

int mW_map_get_x_tile (mW_map * map, int pixel_x);
int mW_map_get_y_tile (mW_map * map, int pixel_y);

int mW_map_location_passable (const mW_map_location * location);
mW_map * mW_map_load_file (const char * filename);

int mW_map_scroll (mW_map * map, int x, int y);

/*
 * Purpose : Scroll the map so it is centered on specific coordinates
 * Returns : 1 when complete
 * mW_error: N/A
 */
int mW_map_scroll_to (mW_map * map, int x, int y);

/*
 * Purpose : Set the display dimensions of a map, used for centering the map
 *           on x,y coordinates
 * Returns : 1 on success
 *           0 if width or height is negative
 * mW_error: N/A
 */
int mW_map_set_display_dimensions (mW_map * map, int display_width, int display_height);

int mW_map_update_offsets (mW_map * map);

int mW_map_passable(mW_map * map, unsigned short x, unsigned short y);

#endif // mW_map_include
